<template>
  <div class="app-container">

    <SearchContent :queryParams="queryParams" @query="handleSearch" @reset="handleReset">
      <el-col :span="6">
        <el-form-item label="工单状态:" prop="workOrderStatus">
          <el-select style="width: 100%;" v-model="queryParams.workOrderStatus" placeholder="请选择工单状态">
            <el-option v-for="item in dict.type.work_order_status" :label="item.label"
                       :value="item.value" :key="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车牌号/车架号:" prop="carNoVinNo">
          <el-input v-model="queryParams.carNoVinNo" clearable placeholder="请输入车牌号/车架号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车辆产权:">
          <el-select filterable v-model="queryParams.propertyRightId" clearable placeholder="请选择车辆产权"
                     style="width: 100%">

            <el-option v-for="item in companyOriginList" :label="item.deptName" :value="item.deptId"
                       :key="item.deptId" />

          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="批次号:" prop="flowBusinessId">
          <el-input v-model="queryParams.flowBusinessId" clearable placeholder="请输入批次号"></el-input>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="车辆归属:">
          <el-select filterable v-model="queryParams.ascriptionId" clearable placeholder="请选择车辆归属"
                     style="width: 100%">

            <el-option v-for="item in companyOriginList" :label="item.deptName" :value="item.deptId"
                       :key="item.deptId" />

          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌/车系/车型:">
          <linkage-select @changeNode="linkageChange" v-model="linkageList" placeholder="请选择品牌/车系/车型"/>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="保险公司:" prop="customerName">
          <el-input v-model="queryParams.customerName" clearable placeholder="请输入保险公司"></el-input>
        </el-form-item>
      </el-col>
    </SearchContent>
    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <e-button type="primary" size="mini" @click="handleAdd" v-hasPermi="['cancellation:cancellationList:add']">
          新增
        </e-button>
        <e-button type="primary" size="mini" @click="handleBatch" :disabled="idsList.length === 0"
                  v-hasPermi="['cancellation:cancellationList:batch']">批量处理
        </e-button>
      </div>
      <table-page :loading="loading" :dataList="dataList" :total="total" :page.sync="queryParams.pageNum"
                  @setSizeCurrent="handleList"
                  :limit.sync="queryParams.pageSize" :isSelect="true" @selectionChange="handleSelectionChange" border>

        <template slot="tableColAc">
          <el-table-column label="操作" align="center" fixed="left" width="100">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="handleView(row, 'view')"
                         v-if="row.workOrderStatus !== 1"
                         v-hasPermi="['cancellation:cancellationList:view']"
              >查看
              </el-button>
              <el-button size="mini" type="text" v-hasPermi="['cancellation:cancellationList:handle']"
                         v-if="row.workOrderStatus == 1 || row.workOrderStatus == 5"
                         @click="handleWork(row)">处理
              </el-button>
            </template>
          </el-table-column>

        </template>

        <template slot="tableCol">
          <el-table-column align="center" label="保险工单编号" prop="workOrderCode" width="180"/>

          <el-table-column align="center" label="批次号" prop="flowBusinessId" show-overflow-tooltip
                           min-width="180"/>

          <el-table-column align="center" label="车牌号" min-width="100">
            <template #default="{row}">
              {{ row.carNo }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="车架号" prop="vinNo" show-overflow-tooltip min-width="180"/>

          <el-table-column align="center" label="品牌/车系/车型" show-overflow-tooltip min-width="320">
            <template #default="{row}">
              {{ row.brandName }}{{ row.seriesName ? '/' + row.seriesName : '' }}{{
                row.modelName ? '/' + row.modelName
                    : ''
              }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="车辆产权" show-overflow-tooltip min-width="160"
                           prop="propertyRightName"/>
          <el-table-column align="center" label="车辆归属" show-overflow-tooltip min-width="160" prop="ascription"/>
          <el-table-column align="center" label="车辆运营" show-overflow-tooltip min-width="120"
                           prop="operationsName"/>
          <el-table-column align="center" label="车辆大区" show-overflow-tooltip min-width="160">
            <template #default="{row}">
              <span v-if="row.region">{{ row.region }} </span>

            </template>
          </el-table-column>

          <el-table-column align="center" label="工单类型" min-width="100">
            <template #default="{row}">
              {{ dictsName(dict.type.work_order_type, row.workOrderType) }}工单
            </template>
          </el-table-column>

          <el-table-column align="center" label="审批状态" show-overflow-tooltip min-width="140" prop="expenseStatus" />

          <el-table-column align="center" label="工单状态" min-width="100">
            <template #default="{row}">
              {{ dictsName(dict.type.work_order_status, row.workOrderStatus) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="审核节点" min-width="120" prop="belongCityName"/>

          <el-table-column align="center" label="车辆状态" min-width="160">
            <template #default="{row}">
              {{ dictsName(dict.type.car_business_type, row.businessType) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="处理人" min-width="120" prop="processedByName"/>

          <el-table-column align="center" label="处理时间" width="140">
            <template #default="{row}">
              {{ row.processedByTime ? $dayjs(row.processedByTime).format('YYYY-MM-DD') : '' }}
            </template>

          </el-table-column>
          <el-table-column align="center" label="工单创建时间" min-width="150" prop="createTime">
            <template slot-scope="{row}">
              {{ row.createTime ? $dayjs(row.createTime).format('YYYY-MM-DD') : '' }}
            </template>
          </el-table-column>

          <el-table-column label="商业险" align="center">
            <el-table-column align="center" label="保单号" min-width="120" prop="policyCode"/>
            <el-table-column align="center" label="保险公司" min-width="120" prop="insuranceCompany"/>
            <el-table-column align="center" label=" 保单时效" min-width="120" prop="policyValidity"/>
            <el-table-column align="center" label="保险到期日" min-width="120" prop="actualEndTime">
              <template slot-scope="{row}">
                {{ row.actualEndTime ? $dayjs(row.actualEndTime).format('YYYY-MM-DD') : '' }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="总费用/元" min-width="120" prop="totalAmount"/>
          </el-table-column>
        </template>
      </table-page>
    </div>
    <addDialog ref="refAddDialog" @change="handleDialog"/>
  </div>
</template>

<script>
import LinkageSelect from "../../components/linkageSelect";
import CitySelect from "../../components/citySelect.vue";
import {getCancellationList} from "@/api/cwgd/cancellation.js"
import SearchContent from "@/components/SearchContent/index.vue";
import addDialog from '../components/addDialog/addDialog.vue'
import { listDept } from "@/api/system/dept";
export default {
  name: "cancellationList",
  dicts: ["car_business_type", "work_order_type", "work_order_status"],
  components: {
    CitySelect,
    LinkageSelect,
    SearchContent,
    addDialog
  },
  computed: {
    dictsName() {
      return (arr, type) => {
        let name = ""
        arr.map((v) => {
          if (type == v.value) {
            name = v.label
          }
        })
        return name
      }
    },
  },
  watch: {},
  data() {
    return {
      showSearch: false,
      loading: false,
      citySelection: [],
      linkageList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo: "",
        belongCityName: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        workOrderStatus: "1",
        customerName: "",
        workOrderType: "11",
        propertyRightId: "",
        ascriptionId: "",
        flowBusinessId:""
      },
      dataList: [],
      total: 0,
      idsList: [],
      companyOriginList: [],
    };
  },
  created() {
    this.handleList()
    this.getCompanyOrigin();
  },
  mounted() {
  },
  methods: {
    getCompanyOrigin() {
      listDept({
        deptType: "2", //组织类型（1部门 2公司）
        regionId: null, //所在城市（省市区id）
      }).then((res) => {
        this.companyOriginList = res.data;
      });
    },
    //新增
    handleAdd() {
      this.$refs.refAddDialog.handleOpen(11)
    },

    handleDialog() {
      this.queryParams.pageNum = 1;
      this.handleList()
    },


    //车辆归属
    citySelectionChange(val) {
      if (val.length > 0) {
        const nodes = Array.from(val, item => ({
          value: item.value,
          label: item.label,
          pv: item.parent.value,
          pl: item.parent.label
        }))
        this.queryParams.belongCityName = nodes[0].label
        this.citySelection = [nodes[0].pv, nodes[0].value]

      } else {
        this.citySelection = val

        this.queryParams.belongCityName = ""
      }
    },

    //品牌/车系/车型
    linkageChange(val) {
      this.linkageList = val
      if (val) {
        this.queryParams.brandName = val[0] ? val[0].name : ''
        this.queryParams.seriesName = val[1] ? val[1].name : ''
        this.queryParams.modelName = val[2] ? val[2].name : ''
      } else {
        this.queryParams.brandName = ""
        this.queryParams.seriesName = ""
        this.queryParams.modelName = ""
      }
      this.$forceUpdate()
    },

    //搜索
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.handleList();
    },

    //重置
    handleReset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo: "",
        belongCityName: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        workOrderStatus: "1",
        customerName: "",
        workOrderType: "11",
        propertyRightId: "",
        ascriptionId: "",
        flowBusinessId:""
      }
      this.citySelection = []
      this.linkageList = []
      this.handleList();
    },

    //查看
    handleView(row) {
      this._toPage({
        path: "/cwgd/handleCancellation",
        name: 'HandleCancellation',
        query: {
          id: row.id,
          type: 'view',
          flowId: row?.flowId || null,
        }
      });
    },

    //多选
    handleSelectionChange(val) {
      this.idsList = val.map(a => {
        return a.id
      })
    },

    //处理
    handleWork(row) {
      this._toPage({
        path: "/cwgd/handleCancellation",
        name: 'HandleCancellation',
        query: {
          id: [row.id],
          type: 'edit',
          flowId: row?.flowId || null,
        }
      });
    },

    //批量处理
    handleBatch() {
      if (this.idsList.length > 0) {
        this._toPage({
          path: '/cwgd/handleCancellation',
          name: 'HandleCancellation',
          query: {
            id: this.idsList,
            type: 'edit'
          }
        })
      } else {
        this.$message({
          message: '请勾选退保工单',
          type: 'warning'
        });
      }
    },


    //获取列表
    async handleList() {
      this.loading = true;
      try {
        const res = await getCancellationList(this.queryParams)
        if (res.code === 200) {
          const data = res
          this.dataList = data.rows || []
          this.total = data.total
        }
      } catch (e) {
        console.error('获取数据失败', e)
      } finally {
        this.loading = false;
      }

    }
  },
};
</script>

<style lang="scss" scoped>
.el-form {
  padding: 18px 20px;
  padding-bottom: 8px;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 16px;

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }

  ::v-deep .el-form-item__label {
    font-size: 14px;
    font-family: 400;
  }
}
</style>



